package admin

import (
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"mall-go/common"
	"mall-go/dto"
	"mall-go/model"
	"mall-go/repository"
	"mall-go/response"
	"mall-go/util"
	"strconv"
	"time"
)

type CarouselsController struct {
	DB *gorm.DB
}

// List 获取轮播图列表
func (receiver CarouselsController) List(ctx *gin.Context) {
	//获取分页数据
	pageNumber, _ := strconv.Atoi(ctx.Query("pageNumber"))
	pageSize, _ := strconv.Atoi(ctx.Query("pageSize"))
	if pageNumber < 1 || pageSize < 10 {
		panic("分页参数异常")
	}
	pageDTO := dto.PageDTO{
		Page:  pageNumber,
		Limit: pageSize,
		Start: (pageNumber - 1) * pageSize,
	}
	carouselRepo := repository.NewCarouselRepo()
	page := carouselRepo.GetCarouselPage(pageDTO)
	response.Success(ctx, gin.H{"carouselVO": page}, "Success")
}

// Save 添加
func (receiver CarouselsController) Save(ctx *gin.Context) {
	//取出管理员id
	user, _ := ctx.Get("adminUser")
	adminUserId := user.(model.NewBeeAdminUser).AdminUserId
	var param dto.CarouselAddParam
	if err := ctx.ShouldBind(&param); err != nil {
		panic(err)
	}
	//构建添加数据库的轮播图entity
	rank, _ := strconv.Atoi(param.CarouselRank)
	carousel := model.Carousel{
		CarouselUrl:  param.CarouselUrl,
		RedirectUrl:  param.RedirectUrl,
		CarouselRank: uint(rank),
		CreateUser:   adminUserId,
		UpdateUser:   adminUserId,
	}
	//添加至数据库中
	if err := receiver.DB.Debug().Create(&carousel).Error; err != nil {
		response.Failure(ctx, nil, "添加失败")
		return
	}
	response.Success(ctx, nil, "添加成功")
}

// Update 修改
func (receiver CarouselsController) Update(ctx *gin.Context) {
	//取出管理员id
	user, _ := ctx.Get("adminUser")
	adminUserId := user.(model.NewBeeAdminUser).AdminUserId
	var param dto.CarouselUpdateParam
	if err := ctx.ShouldBind(&param); err != nil {
		panic(err)
	}
	if param.CarouselId < 1 {
		panic("id必须大于1")
	}
	//构建数据库的轮播图entity
	rank, _ := strconv.Atoi(param.CarouselRank)
	carousel := model.Carousel{
		CarouselId:   uint(param.CarouselId),
		CarouselUrl:  param.CarouselUrl,
		RedirectUrl:  param.RedirectUrl,
		CarouselRank: uint(rank),
		UpdateUser:   adminUserId,
	}
	//根据主键查询数据
	var temp model.Carousel
	receiver.DB.First(&temp, carousel.CarouselId)
	if temp.CarouselId == 0 {
		panic("数据不存在")
	}
	temp.CarouselUrl = carousel.CarouselUrl
	temp.RedirectUrl = carousel.RedirectUrl
	temp.CarouselRank = carousel.CarouselRank
	temp.UpdateTime = model.Time(time.Now())
	temp.UpdateUser = carousel.UpdateUser
	//更新
	if err := receiver.DB.Model(&model.Carousel{}).Update(temp).Error; err != nil {
		response.Failure(ctx, nil, "修改失败")
		return
	}
	response.Success(ctx, nil, "修改成功")
}

// Info 获取单个轮播图信息
func (receiver CarouselsController) Info(ctx *gin.Context) {
	carouselId, _ := strconv.Atoi(ctx.Params.ByName("id"))
	//根据主键查询
	var carousel model.Carousel
	receiver.DB.First(&carousel, carouselId)
	if carousel.CarouselId == 0 {
		panic("数据不存在")
	}
	response.Success(ctx, gin.H{"carousel": carousel}, "Success")
}

// BatchDelete 批量删除
func (receiver CarouselsController) BatchDelete(ctx *gin.Context) {
	var param dto.BatchIdParam
	if err := ctx.Bind(&param); err != nil {
		panic(err)
	}
	if len(param.UserIds) < 1 {
		response.Failure(ctx, nil, "参数异常")
	}
	//执行批量删除
	if err := receiver.DB.Debug().Model(&model.Carousel{}).
		Where("carousel_id in ("+util.Convert(param.UserIds)+")").Update("is_deleted", 1).Error; err != nil {
		panic("删除失败")
	}
	response.Success(ctx, nil, "删除成功")
}

func NewCarouselsController() CarouselsController {
	db := common.GetDB()
	return CarouselsController{DB: db}
}
